package com.lwlk.zdk.util;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;

public class InverseGeographic  {


    public static String BaiDuGeographic(String localhost)throws IOException{


        /**
         * -逆地理编码—百度接口根据经纬度解析地址
         *
         * @param lat_lng
         * @return
         * @throws IOException
         */
        URL url = new URL("http://api.map.baidu.com/reverse_geocoding/v3/?ak=npO3ucRybymGau2miGZhRwYsg40aVpbK&output=json&coordtype=wgs84ll&location="+localhost);
        URLConnection connection = null;
        try {
            connection = url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        /**
         * 然后把连接设为输出模式。URLConnection通常作为输入来使用，比如下载一个Web页。
         * 通过把URLConnection设为输出，你可以把数据向你个Web页传送。下面是如何做：
         */
        connection.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
        out.flush();
        out.close();
        // 一旦发送成功，用以下方法就可以得到服务器的回应：
        String res;
        InputStream l_urlStream;
        l_urlStream = connection.getInputStream();
        BufferedReader in = new BufferedReader(new InputStreamReader(l_urlStream, "UTF-8"));
        StringBuilder sb = new StringBuilder("");
        while ((res = in.readLine()) != null) {
            sb.append(res.trim());
        }
        String str = sb.toString();
        String jsonObject= JSONObject.parseObject(str).getString("result");
        String InverseGeographic= String.valueOf(JSONObject.parseObject(jsonObject).get("formatted_address"));
        return InverseGeographic;
    }


}
